/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getDispatchFee,
  feeApply,
  getRecord,
  getRecordDetailed,
  getExport,
  getStorageFeeSettingList,
  changeStorageFee,
  getStorageFeeStatistics,
  getDistributionList,
  getDistributionDetail,
  getDistributionRecord,
  storageFeeRelease,
  storageFeeReleaseBatch,
  getSubordinatePostStations,
  postStationsOnOff,
  getOpenBrands,
} from '@/services/dispat';
import { message } from 'antd';
import { isArray } from 'lodash';
import { isLegalData } from '@/utils/utils';

export default {
  namespace: 'dispat',

  state: {
    result: null,
    serviceSettings: {
      service_fee: '0',
      original_fee: '0',
      apply_fee: '0',
      check_status: '0',
    },
    feeApplyType: false,
    recordList: {
      list: null,
      pagination: null,
    },
    recordListDetailed: {
      list: null,
      pagination: {
        total: 0,
      },
    },
    summery: {
      total_waybill: '0',
      total_fee: '0',
    },
    summeryDetailed: {
      total_waybill: '0',
      total_fee: '0',
    },
    summeryDayDetailed: {
      total_waybill: '0',
      total_fee: '0',
    },
    storageFeeSetting: {
      list: null,
    },
    statisticsList: {
      list: null,
      pagination: null,
    },
    statisticsDetailList: {
      list: null,
      pagination: null,
    },
    statisticsDayDetailList: {
      list: null,
      pagination: null,
    },
    distributionList: {
      list: null,
      pagination: null,
      summery: {
        deduct_fee: 0,
        frozen_fee: 0,
        send_fee: 0,
      },
    },
    distributionDetail: {
      list: null,
      pagination: null,
    },
    distributionRecord: {
      list: null,
      pagination: null,
    },
    // 服务费下属驿站列表
    postStations: {
      list: null,
      pagination: null,
    },
  },

  effects: {
    *getDispatchFee({ payload, then = () => {} }, { call, put }) {
      const response = yield call(getDispatchFee, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            result: code == 0 && data ? data : null,
            serviceSettings: data,
          },
        });
      }
      then(response);
    },
    *feeApply({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(feeApply, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            feeApplyType: true,
          },
        });
        message.success('已提交');
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *getRecord({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getRecord, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { list = [], total, page, summery } = data;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            recordList: {
              list: isLegalData(list),
              pagination: {
                total,
                current: page,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
            count: total,
            summery,
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },

    *getRecordDetailed({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getRecordDetailed, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { list = [], total, page, summary } = data;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            recordListDetailed: {
              list: isLegalData(list),
              pagination: {
                total,
                current: page,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
            count: total,
            summeryDetailed: summary,
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    *getExport({ payload }, { call, put }) {
      const response = yield call(getExport, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            result: code == 0 && data ? data : null,
            serviceSettings: data,
          },
        });
      }
    },
    // 保管费设置列表
    *getStorageFeeSettingList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getStorageFeeSettingList, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { list = [], total, page } = data;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            storageFeeSetting: {
              list: isLegalData(list),
              pagination: {
                total,
                current: page,
                pageSize: 10,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 保管费设置，修改保管费
    *changeFee({ payload }, { call, put }) {
      const response = yield call(changeStorageFee, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        message.success('修改成功！');
      } else {
        message.error(msg);
      }
    },
    // 保管费统计列表
    *getStorageStatisticsRecord({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getStorageFeeStatistics, payload);
      if (!response) return;

      // console.log('保管费统计：',response, payload)
      const { code, data = {}, msg } = response;
      const { list = [], count, fee_sum_total, in_total, page } = data;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            statisticsList: {
              list: isLegalData(list),
              pagination: {
                total: count,
                current: page,
                pageSize: 10,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
            summery: {
              total_fee: fee_sum_total,
              total_num: in_total,
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },

    // 保管费发放列表
    *getDistributionList({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getDistributionList, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { list = [], total, fee_wait_sum, fee_already_sum, page, fee_decrease_sum } = data;

      if (code === 0) {
        __dva_resolve({
          list: isLegalData(list),
          pagination: {
            total,
            current: page,
            pageSize: 10,
            showQuickJumper: false,
            showSizeChanger: false,
          },
          summery: {
            fee_decrease_sum,
            fee_wait_sum,
            fee_already_sum,
          },
        });
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 保管费发放详情列表
    *getDistributionDetail({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getDistributionDetail, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { list = [], count, page } = data;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            distributionDetail: {
              list: isLegalData(list),
              pagination: {
                total: count,
                current: page,
                pageSize: 10,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 保管费发放记录列表
    *getDistributionRecord({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getDistributionRecord, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { list = [], count, page } = data;

      // console.log("保管费发放记录列表：", response, payload);

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            distributionRecord: {
              list: isLegalData(list),
              pagination: {
                total: count,
                current: page,
                pageSize: 10,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 保管费发放
    *storageFeeRelease({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(storageFeeRelease, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        message.success('发放成功！');
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 保管费批量发放
    *storageFeeReleaseBatch({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(storageFeeReleaseBatch, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        message.success('批量发放成功！');
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 服务费设置，获取下属驿站列表
    *getSubordinatePostStations({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getSubordinatePostStations, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { list = [], total, page, pageSize } = data;

      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            postStations: {
              list: isArray(list) ? list : [],
              pagination: {
                total,
                current: page,
                pageSize: 20,
                showQuickJumper: false,
                showSizeChanger: false,
              },
            },
          },
        });
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 服务费设置，下属驿站开启关闭
    *postStationsOnOff({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(postStationsOnOff, payload);
      if (!response) return;
      const { code, msg } = response;

      if (code == 0) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 当前驿站开通的品牌
    *getOpenBrands({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getOpenBrands, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        __dva_resolve(response);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
